﻿using Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Thought.vCards;

namespace SchemaMonitor.xml
{
    [DataContract]
    [KnownType(typeof(CImg))]
    public class CVCard : CImg
    {
        [DataMember(Order = 1)] public string DisplayName;
        [DataMember(Order = 2)] public string Title;
        [DataMember(Order = 3)] public string FirstName;
        [DataMember(Order = 4)] public string LastName;
        [DataMember(Order = 5)] public string Phones;
        [DataMember(Order = 6)] public string Websites;
        [DataMember(Order = 7)] public string SocialProfiles;
        [DataMember(Order = 8)] public string Photos;


        public CVCard(XmlNode i) : base(i)
        {
            var ms = new MemoryStream(Data);
            var sr = new StreamReader(ms);
            var v = new vCard(sr);
            this.DisplayName = v.DisplayName;
            this.Title = v.Title;
            this.FirstName = v.GivenName;
            this.LastName = v.FamilyName;

            var w = new List<string>();
            foreach (var j in v.Phones)
                w.Add(j.FullNumber + "\t" + j.PhoneType.ToString());
            this.Phones = CUtilities.ListToString(w);

            w = new List<string>();
            foreach (var j in v.Websites)
                w.Add(j.Url + "\t" + j.WebsiteType);
            this.Websites = CUtilities.ListToString(w);

            w = new List<string>();
            foreach (var j in v.SocialProfiles)
                w.Add(j.ProfileUrl + "\t" + j.Username + "\t" + j.ServiceType);
            this.SocialProfiles = CUtilities.ListToString(w);

            w = new List<string>();
            foreach (var j in v.Photos)
                if (j.HasEncodedData)
                    w.Add(j.EncodedData);
                else
                    w.Add(j.Url.AbsoluteUri);
            this.Photos = CUtilities.ListToString(w);
        }
    }
}
